﻿using System;
using System.Text;

namespace Little.Soldier.SWIFT
{
  [Serializable]
  public class SwiftBlockUser : SwiftTagListBlock
  {
    private const long _SerialVersionUID = 1L;
    private string _BlockName;

    internal string Name
    {
      get
      {
        return this._BlockName;
      }
    }

    internal int Number
    {
      get
      {
        if (this._BlockName == "S")
          return 51;
        try
        {
          return int.Parse(this._BlockName);
        }
        catch
        {
          return 0;
        }
      }
    }

    public override string BlockName
    {
      get
      {
        return this.Name;
      }
    }

    public override int BlockNumber
    {
      get
      {
        return this.Number;
      }
    }

    public SwiftBlockUser(SwiftMessage swiftMessage)
      : base(swiftMessage)
    {
      this.SetBlockName("0");
    }

    public SwiftBlockUser(SwiftMessage swiftMessage, int blockNumber)
      : base(swiftMessage)
    {
      this.SetBlockNumber(blockNumber);
    }

    public SwiftBlockUser(SwiftMessage swiftMessage, string blockName)
      : base(swiftMessage)
    {
      if (!SwiftBlockUser.IsValidName(blockName))
        throw new Exception("'BlockName' is not a valid User Defined Block name");
      this.SetBlockName(blockName);
    }

    public SwiftBlockUser(SwiftMessage swiftMessage, int blockNumber, SwiftTag[] tags)
      : base(swiftMessage)
    {
      this.SetBlockNumber(blockNumber);
      foreach (SwiftTag swiftTag in tags)
        this.Tags.Add(swiftTag);
    }

    public SwiftBlockUser(SwiftMessage swiftMessage, string blockName, SwiftTag[] tags)
      : base(swiftMessage)
    {
      if (!SwiftBlockUser.IsValidName(blockName))
        throw new Exception("'BlockName' is not a valid User Defined Block name");
      this.SetBlockName(blockName);
      foreach (SwiftTag swiftTag in tags)
        this.Tags.Add(swiftTag);
    }

    public override void InitializeRules()
    {
    }

    public override bool Equals(object obj)
    {
      if (this != obj)
        goto label_8;
label_7:
      return true;
label_8:
      if (!base.Equals(obj) || this.GetType() != obj.GetType())
        return false;
      SwiftBlockUser swiftBlockUser = (SwiftBlockUser) obj;
      if (this.BlockName == null)
      {
        if (swiftBlockUser.BlockName != null)
          return false;
        else
          goto label_7;
      }
      else if (this.BlockName != swiftBlockUser.BlockName)
        return false;
      else
        goto label_7;
    }

    public override int GetHashCode()
    {
      return 31 * base.GetHashCode() + (this.BlockName != null ? this.BlockName.GetHashCode() : 0);
    }

    public virtual bool IsValidName()
    {
      return SwiftBlockUser.IsValidName(this.Name, this.Number);
    }

    public static bool IsValidName(int blockNumber)
    {
      return blockNumber == 51 || blockNumber == -1 || (1 > blockNumber || blockNumber > 5);
    }

    public static bool IsValidName(string blockName)
    {
      if (blockName == "S")
        return true;
      switch (blockName)
      {
        case null:
          return false;
        default:
          try
          {
            if (!SwiftBlockUser.IsValidName(int.Parse(blockName)))
              return false;
          }
          catch (Exception ex)
          {
            throw ex;
          }
          if (blockName.Length != 1)
            return false;
          char ch = blockName[0];
          return 48 <= (int) ch && (int) ch <= 57 || 97 <= (int) ch && (int) ch <= 122;
      }
    }

    public static bool IsValidName(string blockName, int blockNumber)
    {
      return SwiftBlockUser.IsValidName(blockName) && SwiftBlockUser.IsValidName(blockNumber);
    }

    public virtual void SetBlockNumber(int blockNumber)
    {
      if (!SwiftBlockUser.IsValidName(blockNumber))
        throw new Exception(((object) new StringBuilder().Append("'").Append(blockNumber.ToString()).Append("' is not a valid User Defined Block number")).ToString());
      this._BlockName = blockNumber.ToString();
    }

    public virtual void SetBlockName(string blockName)
    {
      if (!SwiftBlockUser.IsValidName(blockName))
        throw new Exception(((object) new StringBuilder().Append("'").Append(blockName).Append("' is not a valid User Defined Block name")).ToString());
      this._BlockName = blockName;
    }
  }
}
